package com.sx.manage.controller.bank;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sx.common.util.DateUtil;
import com.sx.manage.controller.base.MBaseController;
import com.sx.manage.service.bank.IMBasePayBankInfoStatusService;
import com.sx.manage.service.bank.IMPayBankInfoStatusService;
import com.sx.manage.servlet.ViewAndModel;
import com.sx.model.constants.MDataStatusType;
import com.sx.model.manage.mybatis.form.bank.MPaybankStatusForm;
import com.sx.model.manage.mybatis.vo.bank.MPaybankStatusVO;
import com.sx.model.manage.mybatis.vo.result.ResultBooleanVO;
import com.sx.model.manage.mybatis.vo.result.ResultListVO;
import com.sx.model.manage.mybatis.vo.result.ResultObjectVO;

@Controller
@RequestMapping("/paybank_status")
public class MPaybankStatusController extends MBaseController {

	private Logger log = LoggerFactory.getLogger(this.getClass());

	@Resource(name = "payBankInfoStatusServiceImpl")
	private IMPayBankInfoStatusService payBankStateServiceImpl;

	@Resource(name = "mBasePayBankInfoStatusServiceImpl")
	private IMBasePayBankInfoStatusService mBasePayBankInfoStatusServiceImpl;

	/**
	 * 银行状态维护 编辑
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException	
	 * @throws IOException
	 */
	@RequestMapping(value = "paybank_status_eidt")
	@ResponseBody
	public ResultBooleanVO add_sysQuestionInfo(MPaybankStatusForm form, String start_time, String end_time) {
		log.info("增加银行状态维护信息form={}",form);
		if (null == form) {
			return new ResultBooleanVO("sj.m.common.add.error");
		}
		if (null != start_time) {
			form.setStart_time(DateUtil.StrToDate(start_time));
		}
		if (null != end_time) {
			form.setEnd_time(DateUtil.StrToDate(end_time));
		}
		if (null != form.getId()) {
			log.info("新增银行状态数据form={}",form);
			return this.payBankStateServiceImpl.update(form, this.loginConsumer());
		}
		log.info("维护银行状态数据form={}",form);
		return this.payBankStateServiceImpl.add(form, this.loginConsumer());

	}

	/**
	 * 银行状态维护 查询
	 * 
	 * @param form
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping("/query_byid/{id}")
	@ResponseBody
	public ViewAndModel queryBy_paybank_status_info(MPaybankStatusForm form) {
		ViewAndModel model = new ViewAndModel(request, cache);
		// 查询类
		ResultObjectVO result = mBasePayBankInfoStatusServiceImpl.queryById(form);
		MPaybankStatusVO vo = (MPaybankStatusVO) result.getObjVo();
		model.addObject("paybankStatusInfoVO", vo);
		model.setViewName("sysMaintain/bankMaintainedit");// 修改ftl页面名称
		return model;
	}

	/**
	 * 银行状态维护-查询列表视图
	 * 
	 * @return
	 */
	@RequestMapping("/paybank_status_view")
	public ViewAndModel search_paybank_status_info() {
		ViewAndModel model = new ViewAndModel(request, cache);
		// 如需在添加页需要加载元素在此处增加
		model.setViewName("sysMaintain/bankMaintain");// 修改fitly页面名称
		return model;
	}

	/**
	 * 银行状态维护 数据集合
	 * 
	 * @param form
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/paybank_status_page")
	@ResponseBody
	public ResultListVO<MPaybankStatusVO> search_paybank_status_info(MPaybankStatusForm form) {
		if (null != form && "".equals(form.getBank_name())) {
			form.setBank_name(null);
		}
		return this.mBasePayBankInfoStatusServiceImpl.queryList(form);
	}

	/**
	 * 银行状态维护-启用/停用
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/paybank_update_status")
	@ResponseBody
	public ResultBooleanVO update_status(HttpServletRequest request, HttpServletResponse response) {
		Long id = Long.valueOf(request.getParameter("id"));
		String status = request.getParameter("status");
		if (0 == id) {
			return new ResultBooleanVO("sj.m.common.update.error");
		}
		if (StringUtils.isBlank(status)) {
			return new ResultBooleanVO("sj.m.common.update.error");
		}
		MPaybankStatusForm form = new MPaybankStatusForm();
		form.setId(id);
		if (status.equals(MDataStatusType.DATA_AVAILABLE.toString())) {
			form.setStatus(MDataStatusType.DATA_AVAILABLE);
		} else if (status.equals(MDataStatusType.DATA_DISABLE.toString())) {
			form.setStatus(MDataStatusType.DATA_DISABLE);
		}
		return payBankStateServiceImpl.updateStatus(form, this.loginConsumer());
	}

}
